package com.lt.test.consumer.web.util;

import java.util.*;

public class Sign {
	
	//测试参数是否正确
	public static String sign() {
		String appid = "wx65b4d24b9948e48c";
		String mch_id = "1505594701";
		String device_info = "1000";
		String body = "test";
		String nonce_str = "ibuaiVcKdpRxkhJA";
		//注：key为商户平台设置的密钥key
		String key = "714a8a1ce3434cc5fc08176b07ef3b62";
		SortedMap<Object, Object> parameters = new TreeMap<Object, Object>();
		parameters.put("appid", appid);
		parameters.put("mch_id", mch_id);
		parameters.put("device_info", device_info);
		parameters.put("body", body);
		parameters.put("nonce_str", nonce_str);
		String sign = creatSign(parameters, key);
		return sign;

	}

	public static String creatSign(SortedMap<Object, Object> parameters, String Key) {
		StringBuffer sb = new StringBuffer();
		Set es = parameters.entrySet();
		Iterator<?> it = es.iterator();
		while (it.hasNext()) {
			@SuppressWarnings("rawtypes")
			Map.Entry entry = (Map.Entry) it.next();
			String k = (String) entry.getKey();
			Object v = entry.getValue();
			if (null != v && !"".equals(v) && !"sign".equals(k) && !"key".equals(k)) {
				sb.append(k + "=" + v + "&");
			}

		}
		sb.append("key="+Key);
		String sign = MD5Util.MD5Encode(sb.toString(), "UTF-8").toUpperCase();
		return sign;

	}

	public static void main(String[] args) {

		String sign = sign();

		System.out.println(sign);

	}

}
